package com.redwood.report.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.redwood.report.ItemDecoration.SpaceItemDecoration;
import com.redwood.report.R;
import com.redwood.report.adapter.TongJiItemAdapter;
import com.redwood.report.base.BaseActivity;
import com.redwood.report.entity.ServiceResult;
import com.redwood.report.entity.TongJiBaoBiaoBeen;
import com.redwood.report.entity.TongjiInfo;
import com.redwood.report.entity.TongjiPieChartBeen;
import com.redwood.report.entity.TongjiRongChartBeen;
import com.redwood.report.entity.TongjibarChartBeen;
import com.redwood.report.net.Api;
import com.redwood.report.net.NetUtils;
import com.redwood.report.util.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by sp on 2018/12/12.
 * 统计分析
 */

public class TongjiActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, View.OnClickListener {
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.refresh_layout)
    SwipeRefreshLayout refreshLayout;
    @BindView(R.id.iv_left)
    ImageView ivLeft;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.tv_timernow)
    TextView tvTimernow;
    private TongJiItemAdapter homeAdapter;
    private String startTimer = "";
    private String endTimer = "";
    public static String type ="yunxing";
    public static String dayType = "week";
    private long endTimerjidu=0;
    private long endTimermonth=0;
    private long endWeek=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tongji);
        ButterKnife.bind(this);
        getshijianchui();
        initView();
        initAdapter();
        initData();

    }

    private void initData() {
        onRefresh();
    }

    private void initView() {
        RadioGroup operateRg = findViewById(R.id.operate_rgtongji);
        operateRg.setOnCheckedChangeListener(new MenuCheckedChangeListener());
        ivLeft.setOnClickListener(this);
        ivRight.setOnClickListener(this);
    }

    public TongjiRongChartBeen setChangLiangUI(TongjiInfo.InfoBean.ChangliangBean changliangBean) {
        TongjiRongChartBeen tongjiRongChartBeen = new TongjiRongChartBeen();
        int allChanliang = changliangBean.getHege() + changliangBean.getBaofei() + changliangBean.getBuliang();
        tongjiRongChartBeen.setBaofei((float) changliangBean.getBaofei() / allChanliang);
        tongjiRongChartBeen.setBuliang((float) changliangBean.getBuliang() / allChanliang);
        tongjiRongChartBeen.setHege((float) changliangBean.getHege() / allChanliang);
        return tongjiRongChartBeen;
    }

    public TongjiPieChartBeen setZhuangtaiUI(TongjiInfo.InfoBean.ZhuangtaiBean zhuangtaiUI) {
        TongjiPieChartBeen tongjiPieChartBeen = new TongjiPieChartBeen();
        int allCount = zhuangtaiUI.getDaiji() + zhuangtaiUI.getGuzhang() + zhuangtaiUI.getLixian() + zhuangtaiUI.getYunxing();
        tongjiPieChartBeen.setDaiji((float) zhuangtaiUI.getDaiji() / allCount);
        tongjiPieChartBeen.setLixian((float) zhuangtaiUI.getLixian() / allCount);
        tongjiPieChartBeen.setJingdao((float) zhuangtaiUI.getGuzhang() / allCount);
        tongjiPieChartBeen.setZaixian((float) zhuangtaiUI.getYunxing() / allCount);
        return tongjiPieChartBeen;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_left:
                if(dayType.equals("week")){
                    endWeek=endWeek-3600*24*1000*7L;
                }else if(dayType.equals("month")){
                    endTimermonth=endTimermonth-3600*24*1000L*Utils.getDaysOfMonth(endTimermonth);
                }else if(dayType.equals("jidu")){
                    endTimerjidu=endTimerjidu-3600*24*1000*90L;
                }
                loadChuliZhongByServer();
                break;
            case R.id.iv_right:
                if(dayType.equals("week")){
                    endWeek=endWeek+3600*24*1000*7L;
                }else if(dayType.equals("month")){
                    endTimermonth=endTimermonth+3600*24*1000L*Utils.getDaysOfMonth(endTimermonth);
                }else if(dayType.equals("jidu")){
                    endTimerjidu=endTimerjidu+3600*24*1000*90L;
                }
                loadChuliZhongByServer();
                break;
        }
    }

    public void getshijianchui() {
        endWeek=System.currentTimeMillis();
        endTimermonth=System.currentTimeMillis();
        endTimerjidu=System.currentTimeMillis();
    }

    private class MenuCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
                case R.id.rb_zhou:
                  //  ((TongJiItemAdapter) homeAdapter).setWeekBar("week");
//                    homeAdapter.notifyItemChanged(0);
//                    homeAdapter.notifyItemChanged(1);
                    dayType = "week";
                  loadChuliZhongByServer();
                    break;
                case R.id.rb_month:
                   // ((TongJiItemAdapter) homeAdapter).setWeekBar("month");
//                    homeAdapter.notifyItemChanged(0);
//                    homeAdapter.notifyItemChanged(1);

                    dayType = "month";
                    loadChuliZhongByServer();
                    break;
                case R.id.rb_jidu:
                //    ((TongJiItemAdapter) homeAdapter).setWeekBar("jidu");
//                    homeAdapter.notifyItemChanged(0);
//                    homeAdapter.notifyItemChanged(1);
                    dayType = "jidu";
                    loadChuliZhongByServer();
                    break;
            }
        }
    }

    private void initAdapter() {
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setColorSchemeColors(Color.rgb(47, 223, 189));
        rvList.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvList.addItemDecoration(new SpaceItemDecoration(getActivity()).setSpace(0f).setSpaceColor(getResources().getColor(R.color.blueColorAppKey)));
        homeAdapter = new TongJiItemAdapter(null);
        homeAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {

                }
            }
        });
        homeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {


            }
        });
        homeAdapter.setClickLinster(new TongJiItemAdapter.ClickLinster() {
            @Override
            public void doServer() {
                loadChuliZhongByServer();
            }
        });
        rvList.setAdapter(homeAdapter);
        homeAdapter.setEmptyView(R.layout.loading_view, (ViewGroup) rvList.getParent());
    }

    @Override
    public void onRefresh() {
        loadChuliZhongByServer();
    }

    private void loadChuliZhongByServer() {
        String year="";
        if(dayType.equals("week")){
            startTimer= Utils.timerweek(endWeek-3600*24*1000*7L);
            endTimer=Utils.timerweek(endWeek);
            tvTimernow.setText(startTimer+getResources().getString(R.string.zhi)+endTimer);
            year=Utils.getYear(endWeek+"","");
        }else if(dayType.equals("month")){
            startTimer=  Utils.getFirstDateOfMonth(endTimermonth);;
            endTimer= Utils.getLastDateOfMonth(endTimermonth);
            tvTimernow.setText(Utils.getmonth(endTimermonth+"","")+"月");
            year=Utils.getYear(endTimermonth+"","");
        }else if(dayType.equals("jidu")){
            startTimer=  Utils.getCurrentQuarterStartTime(endTimerjidu); Utils.timerParse(endWeek-3600*24*1000L+"");
            endTimer=Utils.getCurrentQuarterEndTime(endTimerjidu);
            tvTimernow.setText(Utils.getSeason(endTimerjidu)+"季度");
            year=Utils.getYear(endTimerjidu+"","");
        }

        Api.tongjiFenxi(getActivity(),  startTimer, endTimer, year, type, dayType, new NetUtils.NetCallBack<ServiceResult>() {
            @Override
            public void success(ServiceResult rspData) {
                if (rspData.getRecode() == 1) {
                    TongjiInfo tongjiInfo = (TongjiInfo) rspData;
                    if(tongjiInfo==null||tongjiInfo.getInfo()==null){
                        return;
                    }
                    TongjiInfo.InfoBean infoBean = tongjiInfo.getInfo();
                    TongjiInfo.InfoBean.ChangliangBean changliangBean = infoBean.getChangliang();
                    TongjiInfo.InfoBean.ZhuangtaiBean zhuangtaiBean = infoBean.getZhuangtai();
                    List<TongjiInfo.InfoBean.TubiaoBean> tubiaoBeanList = infoBean.getTubiao();
                    TongjibarChartBeen tongjibarChartBeen = new TongjibarChartBeen();
                    if(tubiaoBeanList!=null&&tubiaoBeanList.size()>0){
                        tongjibarChartBeen.setTubiaoBeanList(tubiaoBeanList);
                    List list = new ArrayList();
                    list.add(setZhuangtaiUI(zhuangtaiBean));
                    list.add(setChangLiangUI(changliangBean));
                    list.add(tongjibarChartBeen);
                    addBaoBiao(list, "", true, "", 0,0, 0, 0);
                    List<TongjiInfo.InfoBean.BaobaoBean> baobaoBeanList = infoBean.getBaobao();
                    for (TongjiInfo.InfoBean.BaobaoBean baobaoBean : baobaoBeanList) {
                        addBaoBiao(list, baobaoBean.getBianhao(), false, baobaoBean.getChanliang(), baobaoBean.getGuzhang(), baobaoBean.getLixian(), baobaoBean.getYunxing(), baobaoBean.getDaiji());
                    }
                    homeAdapter.setNewData(list);
                    }else{
                        homeAdapter.setNewData(null);
                    }
                }
                refreshLayout.setRefreshing(false);
            }

            @Override
            public void failed(String msg) {
                refreshLayout.setRefreshing(false);
            }

            @Override
            public void onUsedCache(String msg) {

            }
        }, TongjiInfo.class);
    }

    private void addBaoBiao(List list, String jitai, boolean isTitle, String chanliang, long guzhang, long lixian, long yunxing, long daiji) {
        TongJiBaoBiaoBeen tongJiBaoBiaoBeen = new TongJiBaoBiaoBeen();
        tongJiBaoBiaoBeen.setJitai(jitai);
        tongJiBaoBiaoBeen.setChanliang(chanliang);
        tongJiBaoBiaoBeen.setGuzhang(guzhang);
        tongJiBaoBiaoBeen.setIsTitle(isTitle);
        tongJiBaoBiaoBeen.setLixian(lixian);
        tongJiBaoBiaoBeen.setYunxing(yunxing);
        tongJiBaoBiaoBeen.setDaiji(daiji);
        list.add(tongJiBaoBiaoBeen);
    }


}